package com.shinhye.wisdompeking;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebSettings.TextSize;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class NewsDetailActivity extends Activity {
	private static final String TAG = "NewsDetailActivity";

	private String url;
	private String title;
	private WebSettings webSettings;
	private ProgressBar pb_loading;
	private LinearLayout ll_load_fail;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_news_detail);
		
		url = getIntent().getStringExtra("newsurl");
		title = getIntent().getStringExtra("newstitle");
		
		init();
	}

	private void init() {
		WebView wv_detail = (WebView) findViewById(R.id.news_detail_wv);
		webSettings = wv_detail.getSettings();
		wv_detail.setWebViewClient(new WebViewClient() {
			@Override
			public void onPageFinished(WebView view, String url) {
				pb_loading.setVisibility(View.GONE);
			}
			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				ll_load_fail.setVisibility(View.VISIBLE);
			}
		});
		
		initTitle();
		
		pb_loading = (ProgressBar) findViewById(R.id.pb_loading);
		ll_load_fail = (LinearLayout) findViewById(R.id.ll_load_fail);
		
		Log.i(TAG, "url=" + url);
		wv_detail.loadUrl(url);
	}

	private void initTitle() {
		Button btn_left = (Button) findViewById(R.id.btn_left);
		TextView tv_title = (TextView) findViewById(R.id.txt_title);
		ImageButton ib_textsize = (ImageButton) findViewById(R.id.imgbtn_text);
		ImageButton ib_right = (ImageButton) findViewById(R.id.imgbtn_right);
		
		btn_left.setBackgroundResource(R.drawable.icon_newscenter);
		btn_left.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		tv_title.setText(title);
		ib_textsize.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				webSettings.setTextSize(TextSize.LARGER);
			}
		});
		ib_right.setImageResource(R.drawable.icon_share);
		ib_right.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//分享
				showShare();
			}
		});
	}
	
	private void showShare() {
        ShareSDK.initSDK(this);
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();
        
        // 分享时Notification的图标和文字
        oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title�晏猓∠蟊始恰⒂氏洹⑿畔ⅰ⑽⑿拧⑷巳送蚎Q空间使用
        oks.setTitle(getString(R.string.share));
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        oks.setImagePath("/sdcard/test.jpg");
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");

        // 启动分享GUI
        oks.show(this);
   }
}
